#pragma once
#include <string>
#include "Mysql.hpp" //引入数据库进行业务处理。
#include "Util.hpp"

class Transaction
{
public:
    std::string Login(std::string msg)
    {
        LOG_DEBUG << "Login";
        LOG_DEBUG << "information: " << msg;
        // user=1878048774%40qq.com&pwd=wen2024.
        // 处理登录密码。
        
        // 0、先将账号密码解析出来
        std::string user_name, user_password;
        Util::DistinguishUerAndPassword(msg, &user_name, &user_password);
        LOG_DEBUG << "user: " << user_name << " " << "password: " << user_password;

        // 1、查找User表，查看是否用户存在并且密码正确
        std::string sql_str = "SELECT password FROM user WHERE name =" + user_name + ";";
        std::shared_ptr<sql::ResultSet> ret = _sql_server.ExcuteQuery(sql_str);
        if (ret->next())
        {
            std::string password = ret->getString("password");

            if (password != user_password)
            {
                return "PASSWORD_ERROR";
            }
            else
            {
                return "PERMIT_LOGIN";
            }
        }
        else
        {
            return "USER_UNEXIST";
        }
    }

private:
    MySQL _sql_server;
};
